export interface Transfer {
    type: string;
    toAddress: string;
    fromTag: string;
    fromAddressType: string;
    toAddressType: string;
    transactionHash: string;
    timestamp: number;
    outAmount: number;
    outTokenAddress: string;
    outSymbol: string;
    action: string;
    outusdt: number;
    high: number;
    inStable: boolean;
    outStable: boolean;
}

export interface Swap {
    type: string;
    address: string;
    fromAddressType: string;
    toTag: string;
    toAddressType: string;
    transactionHash: string;
    timestamp: number;
    inAmount: number;
    inTokenAddress: string;
    inSymbol: string;
    outAmount: number;
    outTokenAddress: string;
    outSymbol: string;
    action: string;
    inusdt: number;
    outusdt: number;
    high: number;
    inStable: boolean;
    outStable: boolean;
}

const withdraw = "withdraw"; //提现
const deposit = "deposit"; //充值
const transfer = "transfer"; //转账
const swap = "swap"; //交易

export function coverAction(obj: Transfer | Swap) {
    //转账
    if (obj.type == transfer) {
        //充值
        if (obj.action == deposit) {
            if (obj.outStable) {
                return {
                    type: "CEX",
                    up: true
                };
            } else {
                return {
                    type: "CEX",
                    up: false
                };
            }
        }
        //提现
        else if (obj.action == withdraw) {
            if (!obj.outStable) {
                return {
                    type: "CEX",
                    up: true
                };
            } else {
                return {
                    type: "CEX",
                    up: false
                };
            }
        }
        //transfer
        else {
            return { type: "NORMAL" };
        }
    }
    //交易
    else if (obj.type == swap) {
        if (obj.outStable) {
            return {
                type: "DEX",
                up: true
            };
        } else {
            return {
                type: "DEX",
                up: false
            };
        }
    }

    return {
        type: "NORMAL"
    };
}

export function getMarkObj(data: any) {
    if (data.type == "swap") {
        return getSwapObj(data as Swap);
    }
    return getTransferObj(data as Transfer);
}

export function getSwapObj(val: Swap) {
    let title, act, up;
    if (val.action == "buy") {
        //买
        title = "buy";
        act = "in";
        up = true;
    } else {
        //卖
        title = "sell";
        act = "out";
        up = false;
    }
    return { title, act, up };
}

export function getTransferObj(val: Transfer) {
    let title, act, up;
    if (val.action == "withdraw") {
        //提现
        title = "withdraw";
        act = "out";
        up = true;
    } else {
        //充值
        title = "deposit";
        act = "out";
        up = false;
    }
    return { title, act, up };
}

export function sortByKey(ary: any[], key: string, isUp = true) {
    let direction = isUp ? 1 : -1;
    return ary.sort((a, b) => {
        if (a[key] < b[key]) return -1 * direction;
        if (a[key] > b[key]) return 1 * direction;
        return 0;
    });
}
